home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-02 / fileread.zip / TEST2.PAS < prev   
Pascal/Delphi Source File  |  1992-04-16  |  3KB  |  89 lines

  1.  
  2. { **************************************************************************
  3.   *                                                                        *
  4.   *                         FileRead Test Program #2                       *
  5.   *                         ========================                       *
  6.   *                                                                        *
  7.   *  Description:                                                          *
  8.   *                                                                        *
  9.   *       This program is similar to program #1 with the exception that    *
  10.   *  instead of using a record as the storage, we are using a word.        *
  11.   *  This shows that you can use almost any type to store in a file, as    *
  12.   *  long as what you are storing in the file stays consistent.            *
  13.   *                                                                        *
  14.   ************************************************************************** }
  15.  
  16. Program Test_FileRead_2;
  17.  
  18. uses
  19.   Crt,FileRead;
  20.  
  21. Procedure Main;
  22. var
  23.   MyFile : TFreadPtr;  { an instance of the TFread object }
  24.   i      : integer;    { loop variable }
  25.   temp   : string;     { temp string for processing integers }
  26.   f      : file;       { temp file for deleting old version of TOM.GAG }
  27.   Number : Word;       { instead of a record, let's create a file of words!}
  28. Begin
  29.  
  30.   { Let's initialize a file object and create a new file. }
  31.   New(MyFile,Init('INFO.DAT',Create,SizeOf(Number)));
  32.  
  33.   { Let's do some stuff with our new file. }
  34.   with Myfile^ do
  35.   begin
  36.     randomize;
  37.     for i:=1 to 100 do
  38.     begin
  39.       Number:=random(65535);
  40.       AppendRec(Number);
  41.     end;
  42.     { Try to rename the file. }
  43.     if not RenameFile('TOM.GAG') then
  44.     begin
  45.       { If we cannot, then delete previous version of TOM.GAG. }
  46.       Assign(f,'TOM.GAG');
  47.       erase(f);
  48.       { And try one more time. }
  49.       if not RenameFile('TOM.GAG') then
  50.       begin
  51.         { if we cannot rename this time, then just give up. }
  52.         Writeln('Still having trouble renaming file!');
  53.         halt(1);
  54.       end;
  55.     end;
  56.     { If we can rename the file, then read the first record. }
  57.     ReadRec(Number,0);
  58.     if ReadError then
  59.       { if a read error occured, then just close and quit. }
  60.       writeln('Oops, dude, and error hath occurred!')
  61.     else
  62.     begin
  63.       { go through the file sequentially forward, printing the name
  64.         field of the particular record. }
  65.       while not ReadError do
  66.       begin
  67.         Writeln(GetCurrent:4,':  ',Number);
  68.         ReadNext(Number);
  69.       end;
  70.       writeln;
  71.       write('Press a key to go backwards!');
  72.       readln;
  73.       writeln;
  74.       { Let's go backward now! }
  75.       ReadCurrent(Number);
  76.       while not ReadError do
  77.       begin
  78.         Writeln(GetCurrent:4,':  ',Number);
  79.         ReadPrevious(Number);
  80.       end;
  81.     end;
  82.   end;
  83.   { Okay, close file and clean house. }
  84.   Dispose(Myfile,Done);
  85. End;
  86.  
  87. Begin
  88.   Main;
  89. End.